package com.app.studentActivity.fragment;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.app.R;
import com.app.adapter.NoticeAdapter;
import com.app.bean.NoticeBean;
import com.app.util.DBHelper1;
import com.app.util.DateUtil;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {

    private RecyclerView recy;
    private List<NoticeBean> list;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        initView(view);
        initData();
        getRecy();

        return view;
    }

    private void getRecy() {
        recy.setAdapter(new NoticeAdapter(getContext(), list));
    }

    private void initData() {
        list=new ArrayList<>();
        DBHelper1 dbHelper1 = new DBHelper1(getContext());
        SQLiteDatabase db = dbHelper1.getReadableDatabase();
        Cursor cursor = db.query("notice", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            NoticeBean bean = new NoticeBean();
            bean.setName(cursor.getString(cursor.getColumnIndex("name")));
            bean.setContent(cursor.getString(cursor.getColumnIndex("content")));
            bean.setTime(cursor.getString(cursor.getColumnIndex("date")));
            bean.setPic(cursor.getBlob(cursor.getColumnIndex("pic")));
            list.add(bean);
        }
        cursor.close();
        db.close();
    }

    private void initView(View view) {
        Toolbar toolbar = view.findViewById(R.id.toolbar);
        toolbar.getChildAt(0).setVisibility(View.GONE);
        ((TextView) toolbar.getChildAt(1)).setText("首页");
        recy = view.findViewById(R.id.recy);
        recy.setLayoutManager(new LinearLayoutManager(getContext()));
    }
}